package com.sg.service.base.component.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sg.common.util.CommonFunctionHelper;
import com.sg.dto.base.component.req.BatchQueryStepReq;
import com.sg.entity.OmsAssistStep;
import com.sg.service.base.component.MOmsAssistStepService;
import com.wicket.okrcomponent.common.dto.FieldInfo;
import com.wicket.okrcomponent.common.exception.BizException;
import com.wicket.okrcomponent.dal.mapper.mbg.OmsAssistStepMapper;
import org.apache.skywalking.apm.toolkit.trace.Tag;
import org.apache.skywalking.apm.toolkit.trace.Tags;
import org.apache.skywalking.apm.toolkit.trace.Trace;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author ：
 * @version 1.0
 * @since 2022/1/19 15:39
 */
@Service
public class MOmsAssistStepServiceImpl
        extends ServiceImpl<OmsAssistStepMapper, OmsAssistStep>
        implements MOmsAssistStepService {

    @Autowired
    OmsAssistStepMapper omsAssistStepMapper;

    /**
     * 2-9-01查询步骤详情[864]
     * gen by moon at 10/24/2022, 12:58:29 AM
     */
    @Trace(operationName = "2-9-01查询步骤详情")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public OmsAssistStep queryStepDetail(QueryStepDetailReq reqDto) {

        try {

            QueryWrapper<OmsAssistStep> queryWrapper = new QueryWrapper<>();
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, new ArrayList<>());
            queryWrapper.orderByDesc("create_time");
            List<OmsAssistStep> retList = super.list(queryWrapper);
            if (retList != null && retList.size() > 0) {
                OmsAssistStep retData = retList.get(0);

                return retData;
            }
            return null;
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-9-01查询步骤列表[863]
     * gen by moon at 10/24/2022, 12:58:29 AM
     */
    @Trace(operationName = "2-9-01查询步骤列表")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAssistStep> queryStepList(QueryStepListReq reqDto) {

        try {
            QueryWrapper<OmsAssistStep> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");

            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAssistStep.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAssistStep.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAssistStep.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAssistStep>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }

    /**
     * 2-9-01删除步骤[867]
     * gen by moon at 10/24/2022, 12:58:31 AM
     */
    @Trace(operationName = "2-9-01删除步骤")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean deleteStep(String assistStepId) {

        return super.removeById(assistStepId);
    }

    /**
     * 2-9-01修改步骤[866]
     * gen by moon at 10/24/2022, 12:58:30 AM
     */
    @Trace(operationName = "2-9-01修改步骤")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public boolean updateStep(OmsAssistStep omsAssistStep) {
        omsAssistStep.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAssistStep != null && omsAssistStep.getSubjectLifeCycle() != null && omsAssistStep.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAssistStep.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        if (omsAssistStep != null && omsAssistStep.getSubjectLifeCycle() != null && omsAssistStep.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAssistStep.setArchiveInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//ARCHIVE_CURRENT_INDUCTIONID
        }
        omsAssistStep.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAssistStep != null && omsAssistStep.getSubjectLifeCycle() != null && omsAssistStep.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAssistStep.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        if (omsAssistStep != null && omsAssistStep.getSubjectLifeCycle() != null && omsAssistStep.getSubjectLifeCycle().equals("ARCHIVING")) {
            omsAssistStep.setArchiveTime(CommonFunctionHelper.getCurrentTime());//HISTORICAL_ARCHIVING
        }
        return super.updateById(omsAssistStep);
    }

    /**
     * 2-9-01新增步骤[865]
     * gen by moon at 10/24/2022, 12:58:30 AM
     */
    @Trace(operationName = "2-9-01新增步骤")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public String addStep(OmsAssistStep omsAssistStep) {
        omsAssistStep.setAssistStepId(CommonFunctionHelper.getUid());//DISTRIBUTED_ID
        omsAssistStep.setIsArchive("FALSE");
        omsAssistStep.setIsValid("FALSE");
        omsAssistStep.setOperationInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        omsAssistStep.setCreateInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//CURRENT_INDUCTIONID
        if (omsAssistStep != null && omsAssistStep.getSubjectLifeCycle() != null && omsAssistStep.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAssistStep.setReleaseInductionId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecInductionRecordId());//RELEASED_CURRENT_INDUCTIONID
        }
        omsAssistStep.setOperateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        omsAssistStep.setCreateTime(CommonFunctionHelper.getCurrentTime());//SYSTEM_CURRENT_TIME
        if (omsAssistStep != null && omsAssistStep.getSubjectLifeCycle() != null && omsAssistStep.getSubjectLifeCycle().equals("CURRENTLY_VALID")) {
            omsAssistStep.setReleaseTime(CommonFunctionHelper.getCurrentTime());//RELEASE_SYSTEM_CURRENT_TIME
        }
        omsAssistStep.setSpaceId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecSpaceId());//CURRENT_SPACEID
        omsAssistStep.setAppId(CommonFunctionHelper.getBaseInfoFromLocal().getSpecAppId());//CURRENT_APPID
        if (super.save(omsAssistStep)) {
            return omsAssistStep.getAssistStepId();
        } else {
            return "-1";
        }

    }

    /**
     * 2-9批量查询步骤[4998]
     * gen by moon at 10/24/2022, 12:58:32 AM
     */
    @Trace(operationName = "2-9批量查询步骤")
    @Tags({@Tag(key = "参数", value = "arg[0]"),
            @Tag(key = "返回值", value = "returnedObj")})
    @Override
    @Transactional(rollbackFor = Exception.class)
    public List<OmsAssistStep> batchQueryStep(BatchQueryStepReq reqDto) {

        try {
            QueryWrapper<OmsAssistStep> queryWrapper = new QueryWrapper<>();
            reqDto.setIsValid("FALSE");
            if (reqDto.getStepList() == null || reqDto.getStepList().isEmpty()) {
                return new ArrayList<>();
            }
            queryWrapper.in("step_name_code", reqDto.getStepList());
            Map<String, Object> fieldInfoMap = null;
            fieldInfoMap = CommonFunctionHelper.objectToMapForLamBda(reqDto);
            List<FieldInfo> compareList = CommonFunctionHelper.getRangeField(OmsAssistStep.class);
            CommonFunctionHelper.buildQueryWrapperCompare(queryWrapper, compareList, fieldInfoMap);
            List<FieldInfo> likeList = CommonFunctionHelper.getLikeFiled(OmsAssistStep.class);
            CommonFunctionHelper.buildQueryWrapperLike(queryWrapper, likeList, fieldInfoMap);
            likeList.addAll(compareList);
            CommonFunctionHelper.buildQueryWrapperEq(queryWrapper, fieldInfoMap, likeList);
            List<FieldInfo> orderList = CommonFunctionHelper.getOrderList(OmsAssistStep.class);
            //CommonFunctionHelper.buildQueryWrapperOrder(queryWrapper, orderList);
            queryWrapper.orderByAsc("order_number");
            queryWrapper.orderByDesc("operate_time");
            return ((List<OmsAssistStep>) super.list(queryWrapper));
        } catch (Exception e) {
            e.printStackTrace();
            throw new BizException("-1", e, false);
        }
    }
}
